home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4424 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.9 KB  |  54 lines

  1. Path: news.kei.com!ub!newserve!rebecca!rpi!not-for-mail
  2. From: floydb1@lib105.its.rpi.edu (Barry B Floyd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: cin.flush( )???
  5. Date: 29 Jan 1996 14:41:36 -0500
  6. Organization: Rensselaer Polytechnic Institute, Troy, NY.
  7. Message-ID: <4ej7tg$fe2@lib105.its.rpi.edu>
  8. References: <00001a81+0000956f@msn.com>
  9. NNTP-Posting-Host: lib105.its.rpi.edu
  10. X-newsreader: xrn 7.04-beta-11
  11.  
  12.  
  13. In article <00001a81+0000956f@msn.com>, RMarlowe@msn.com (Roger Marlowe) writes:
  14. |>     Does anyone know how to clear (flush) the cin stream?
  15. |>     I have a persistant problem with unwanted '\n's hanging around in there.
  16. |>     flush(รก) seems to work well with cout but where is it's counterpart for cin?
  17.  
  18. ...ignore ( len, delim ) ;
  19.  
  20. I have had varying degrees of success with this. I believe my lack
  21. of success is due to my lack of understanding the underlying buf
  22. environment. For example, ...ignore ( 1024, EOF ) in my mind should
  23. do nothing if the cin buffer is empty - but in practice ( GNU g++ )
  24. it seems to ignore the next time I use 'cin >> variable'. Checking
  25. for EOF before ignore doesn't always seem to work either - limited
  26. tests seem to indicate that the variable type matters.
  27.  
  28. For now I have ignored my lack of understanding and implemented
  29. something that works in a limited context. The following works as
  30. expected (if there is ZERO or ONE extra '\n' in cin's buf):
  31.  
  32. while ( TRUE )    
  33. {
  34.     String name = STR_EOS ;
  35.  
  36.     cout    << "   Name: " ;
  37.  
  38.     if ( cin.peek () != EOF )
  39.         cin.ignore ( 80, '\n' ) ;
  40.  
  41.     cin >> name ;
  42. }
  43.  
  44.  
  45. If anyone has some diverse source code examples of cin usage please
  46. post references to the group.
  47.  
  48. barry
  49. -- 
  50. +--------------------------------------------------------------------+ 
  51. | Barry B. Floyd                   \\\               floydb1@rpi.edu |
  52. | RPI Alum. '84 '87 '88              \\\                             |
  53. +--------------------------------------------------------------------+
  54.